package com.zhilun;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.HttpResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhilun.check.entity.ZhiLunPositionDataParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// SELECT  CONCAT(r.license_plate, '-', r.device_id)   from  device_register r  join  device_info   i on  i.device_id = r.device_id    WHERE i.manufacturer_id = 7   and r.license_plate != ''

public class CheckDevcie {
    public static void main(String[] args) throws IOException {
        String url = "https://motorcade.zhilun.com/smartApi/motorcade-api/devices/operator/list?pageSize=5000";
        List<String> zhiLun = new ArrayList<>();

        String filePath = "C:\\Users\\hcf\\IdeaProjects\\dache-test\\service-hi\\src\\main\\resources\\sensor-bind\\device.txt"; // 文件路径

        List<String> daChe = Files.readAllLines(Paths.get(filePath));

        // 设置请求头
        HttpResponse response = HttpUtil.createGet(url)
                .header("Accept", "application/json, text/plain, */*")
                .header("Accept-Language", "zh-cn")
                .header("Cache-Control", "no-cache")
                .header("Connection", "keep-alive")
                .header("Cookie", "acw_tc=0b32807617304447067993750e38db39d17336728142ef4c27ea96335e1fed")
                .header("Pragma", "no-cache")
                .header("Referer", "https://motorcade.zhilun.com/server/stock/terminal")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-origin")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")
                .header("access_token", "5d02dec6-704a-4098-8561-d4be358234ba")
                .header("role_type", "3")
                .header("sec-ch-ua", "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("system_version", "3")
                .execute();

        // 获取响应内容
        String responseBody = response.body();
        JSONObject jsonObject =JSONObject.parseObject(responseBody);
        JSONArray dataArray = jsonObject.getJSONArray("data");

        // 遍历数据并提取需要的信息
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject item = dataArray.getJSONObject(i);

            String carrierNo = item.getString("carrierNo");
            String deviceId = item.getString("sn");
            if (!StringUtils.isEmpty(deviceId)) {
                zhiLun.add(carrierNo +"-"+deviceId);

            }


        }

        Collection<String> subtractList = CollectionUtils.subtract(zhiLun, daChe);
        Collection<String> subtractList2 = CollectionUtils.subtract(daChe, zhiLun);
        List<ZhiLunPositionDataParam> list = new ArrayList<>();
       // List<ZhiLunPositionDataParam> list2 = new ArrayList<>();
        for (String s : subtractList) {
            String[] split = s.split("-");
            ZhiLunPositionDataParam zhiLunPositionDataParam = new ZhiLunPositionDataParam();
            zhiLunPositionDataParam.setLicensePlate(split[0]);
            zhiLunPositionDataParam.setDeviceId(split[1]);
            zhiLunPositionDataParam.setSource(null);
            list.add(zhiLunPositionDataParam);
        }

        // 填充 list2
        for (String s : subtractList2) {
            String[] split = s.split("-");
            ZhiLunPositionDataParam zhiLunPositionDataParam = new ZhiLunPositionDataParam();
            zhiLunPositionDataParam.setLicensePlate(split[0]);
            zhiLunPositionDataParam.setDeviceId(split[1]);
            zhiLunPositionDataParam.setSource(null);
            list.add(zhiLunPositionDataParam);
        }

        // 导出到两个不同的 Excel 文件
        exportToExcel(list, "主机.xlsx");
       // exportToExcel(list2, "主机-知轮无大车有.xlsx");
        //知轮有 大车无
        System.out.println("知轮有 大车无:");
        System.out.println(subtractList);
        //知轮无 大车有
        System.out.println("知轮无 大车有:");
        System.out.println(subtractList2);

        System.out.println(1);
    }


    // 导出 Collection<ZhiLunPositionDataParam> 到 Excel 文件的方法
    public static void exportToExcel(List<ZhiLunPositionDataParam> data, String fileName) {
        EasyExcel.write(fileName, ZhiLunPositionDataParam.class)  // 写入到指定的文件，指定 ZhiLunPositionDataParam 类
                .sheet("Sheet1")                // 表格的名称
                .doWrite(data);                 // 写入数据
    }
}
